package com.iqik.rabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableRabbit
public class RabbitConfigure {

    @Bean(name = "rabbitMessageListenerContainer")
    public DirectMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory) {
        DirectMessageListenerContainer container = new DirectMessageListenerContainer(connectionFactory);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setPrefetchCount(5);
        container.setConsumersPerQueue(5);
        container.setMessagesPerAck(1);
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        container.setTaskExecutor(taskExecutor);
        return container;
    }

   /* @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }*/

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    /**
     * 订单消息队列
     */
    @Bean
    public Queue queue() {
        return new Queue("queue.order");
    }

    /**
     * 订单延时消息队列
     */
    @Bean
    public Queue delayQueue() {
        Map<String, Object> map = new HashMap<>();
        // 消息过期时间
        map.put("x-message-ttl", 60_000);
        // 通过死信交换器发送到死信队列，死信交换器的名称，DLX
        map.put("x-dead-letter-exchange", "ex.dlx.order");
        // 死信交换器的路由键
        map.put("x-dead-letter-routing-key", "routing.key.dlx.order");
        return new Queue("queue.ttl.order", true, false, false, map);
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue dlxQueue() {
        Map<String, Object> map = new HashMap<>();
        return new Queue("queue.dlx.order", true, false, false, map);
    }

    /**
     * 延时交换器
     */
    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange("ex.ttl.order", true, false);
    }

    /**
     * 死信交换
     */
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("ex.dlx.order", true, false);
    }

    /**
     * 正常交换器
     */
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange("ex.order", true, false);
    }

    /**
     * 延时 队列绑定交换器
     */
    @Bean
    public Binding delayBinding(Queue delayQueue, DirectExchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with("routing.key.ttl.order");
    }

    /**
     * 死信 队列绑定交换器
     */
    @Bean
    public Binding dlxBinding(Queue dlxQueue, DirectExchange dlxExchange) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("routing.key.dlx.order");
    }

    /**
     * 正常消息队列绑定交换器
     */
    @Bean
    public Binding receiveBinding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("routing.key.order");
    }

}
